<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Three.js Demo</title>
  <style>
    html, body {
      margin: 0;
      padding: 0;
      height: 100%;
    }
  </style>
</head>
<body>
  
  <script src="../lib/three.js"></script>
  <script src="../lib/OrbitControls.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
  <script src="../lib/onEvent.js"></script>
  <script src="../lib/ThreeBSP.js"></script>
  <script src="../lib/FirstPersonControls.js"></script>
  <script>
    // 初始化渲染器
    const renderer = new THREE.WebGLRenderer({
      antialias: true,
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMapEnabled = true;
    document.body.appendChild(renderer.domElement);

    // 创建场景
    const scene = new THREE.Scene();

    // 创建相机
    const camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
    camera.position.set(20, 40, 50);
    camera.lookAt(scene.position);
    scene.add(camera);

    // 添加点光源
    const pointLight = new THREE.PointLight(0xffffff);
    pointLight.position.set(30, 40, 50);
    scene.add(pointLight);
    const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);
    scene.add(pointLightHelper);

    // 添加环境光
    const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
    scene.add(ambientLight);

    // 事件相关
    const threeOnEvent = new THREE.onEvent(scene, camera);

    // 显示辅助坐标系
    var axesHelper = new THREE.AxesHelper(100);
    scene.add( axesHelper );

    // 创建材质加载器
    var loader = new THREE.TextureLoader();

    // 添加物体
    initObjects();

    function initObjects() {
      paintFloor(); // 绘制地板
      buildWall(40, 10, 2, 0, 5, 20); // 前面墙
      buildWall(42, 10, 2, -20, 5, 0, 0, 1/2); // 左面墙
      buildWall(42, 10, 2, 20, 5, 0, 0, 1/2); // 右面墙
      const backWall = buildWall(40, 10, 2, 0, 5, -20, 0, 0, 0, false); // 后面墙
      const door = buildGlass(5, 8, 2, 0, 4, -20); // 门
      const backWallWithHole = createResultMesh(backWall, door);
    }

    function paintFloor() {
      loader.load('../texture/floor.png', function(texture) {
        // x 和 y 超过图片像素之后重复绘制图片
        texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
        // 设置地板重复绘制的密度是 1 * 1
        texture.repeat.set(1, 1);
        // 创建普通的平面几何体
        var gemotery = new THREE.PlaneGeometry(40, 40);
        // 设置材质是双面材质
        var material = new THREE.MeshLambertMaterial({
            map: texture,
            side: THREE.DoubleSide
        });
        // 创建网格对象
        var mesh = new THREE.Mesh(gemotery, material);
            // mesh.position.y = 0;
            mesh.rotation.x = Math.PI / 2;
        // 绑定事件
        mesh.on("click", function(m) {
          console.log('点击了地板', m);
        });
        scene.add(mesh);
      });
    }

    function buildWall(width, height, depth, x, y, z, rotationX = 0, rotationY = 0, rotationZ = 0, draw = true) {
      var texture = loader.load('../texture/wall.png');
      // x 和 y 超过图片像素之后重复绘制图片
      texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
      // 设置墙面重复绘制的密度是 1 * 1
      texture.repeat.set(1, 1);
      // 创建长方体几何体
      var gemotery = new THREE.BoxGeometry(width, height, depth);
      // 设置材质
      var material = new THREE.MeshLambertMaterial({
          map: texture,
          side: THREE.DoubleSide
      });
      // 创建网格对象并设定位置
      var mesh = new THREE.Mesh(gemotery, material);
      mesh.position.set(x, y, z);
      mesh.rotation.set(Math.PI * rotationX, Math.PI * rotationY, Math.PI * rotationZ);
      // 绑定事件
      mesh.on("click", function(m) {
        console.log('点击了墙', m);
      });
      if(draw) {
        scene.add(mesh);
      }
      return mesh;
    }

    function buildGlass(width, height, depth, x, y, z, rotationX = 0, rotationY = 0, rotationZ = 0, draw = true) {
      // 创建长方体几何体
      var gemotery = new THREE.BoxGeometry(width, height, depth);
      // 设置材质
      var material = new THREE.MeshBasicMaterial({
          color : 0x58ACFA,
          transparent : true,
          opacity : 0.6
      });
      // 创建网格对象并设定位置
      var mesh = new THREE.Mesh(gemotery, material);
      mesh.position.set(x, y, z);
      mesh.rotation.set(Math.PI * rotationX, Math.PI * rotationY, Math.PI * rotationZ);
      // 绑定事件
      mesh.on("click", function(m) {
        console.log('点击了墙', m);
      });
      if(draw) {
        scene.add(mesh);
      }
      return mesh;
    }

    function createResultMesh(srcMesh, destMesh) {
      var srcBSP = new ThreeBSP(srcMesh);
      var despBSP = new ThreeBSP(destMesh);
      // 进行差集计算
      var resultBSP = srcBSP.subtract(despBSP);
      // 从BSP对象内获取到处理完后的mesh模型数据
      var result = resultBSP.toMesh(srcMesh.material);
      // 更新模型的面和顶点的数据
      result.geometry.computeFaceNormals();
      result.geometry.computeVertexNormals();
      scene.add(result);
      return result;
    }

    const controls = new THREE.OrbitControls( camera, renderer.domElement );
    // const firstPersonControls = new THREE.FirstPersonControls(camera);
    // firstPersonControls.lookSpeed = 0.2; //鼠标移动查看的速度
    // firstPersonControls.movementSpeed = 20; //相机移动速度
    // firstPersonControls.noFly = true;
    // firstPersonControls.constrainVertical = true; //约束垂直
    // firstPersonControls.verticalMin = 1.0;
    // firstPersonControls.verticalMax = 2.0;
    // firstPersonControls.lon = -100; // 进入初始视角x轴的角度
    // firstPersonControls.lat = 0; // 初始视角进入后y轴的角度
    
    const clock = new THREE.Clock();
    function render() {
      requestAnimationFrame(render);
      controls.update();
      // firstPersonControls.update(clock.getDelta());
      renderer.render(scene, camera);
    }

    render();
    
  </script>
</body>
</html>